home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / scm / slib / scmacro.scm < prev    next >
Text File  |  1999-04-19  |  3KB  |  120 lines

  1. ;"scmacro.scm", port for Syntactic Closures macro implementation -*- Scheme -*-
  2. ;Copyright (C) 1992, 1993, 1994 Aubrey Jaffer
  3. ;
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7. ;
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10. ;
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14. ;
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. ;;;; Syntaxer Output Interface
  21.  
  22. (define syntax-error slib:error)
  23.  
  24. (define impl-error slib:error)
  25.  
  26. (define (append-map procedure . lists)
  27.   (apply append (apply map (cons procedure lists))))
  28.  
  29. (define *counter* 0)
  30.  
  31. (define (make-name-generator)
  32.   (let ((suffix-promise
  33.      (make-promise
  34.       (lambda ()
  35.         (string-append "."
  36.                (number->string (begin
  37.                          (set! *counter* (+ *counter* 1))
  38.                          *counter*)))))))
  39.     (lambda (identifier)
  40.       (string->symbol
  41.        (string-append "."
  42.               (symbol->string (identifier->symbol identifier))
  43.               (promise:force suffix-promise))))))
  44.  
  45. (define (output/variable name)
  46.   name)
  47.  
  48. (define (output/literal-unquoted datum)
  49.   datum)
  50.  
  51. (define (output/literal-quoted datum);was output/constant (inefficient)
  52.   `(QUOTE ,datum))
  53.  
  54. (define (output/assignment name value)
  55.   `(SET! ,name ,value))
  56.  
  57. (define (output/top-level-definition name value)
  58.   `(DEFINE ,name ,value))
  59.  
  60. (define (output/conditional predicate consequent alternative)
  61.   `(IF ,predicate ,consequent ,alternative))
  62.  
  63. (define (output/sequence expressions)
  64.   (if (null? (cdr expressions))
  65.       (car expressions)
  66.       `(BEGIN ,@expressions)))
  67.  
  68. (define (output/combination operator operands)
  69.   `(,operator ,@operands))
  70.  
  71. (define (output/lambda pattern body)
  72.   `(LAMBDA ,pattern ,body))
  73.  
  74. (define (output/delay expression)
  75.   `(DELAY ,expression))
  76.  
  77. (define (output/unassigned)
  78.   `'*UNASSIGNED*)
  79.  
  80. (define (output/unspecific)
  81.   `'*UNSPECIFIC*)
  82.  
  83. (require 'promise)            ; Portable support for force and delay.
  84. (require 'record)
  85. (require 'synchk)            ; Syntax checker.
  86.  
  87. ;;; This file is the macro expander proper.
  88. (slib:load (in-vicinity (library-vicinity) "synclo"))
  89.  
  90. ;;; These files define the R4RS syntactic environment.
  91. (slib:load (in-vicinity (library-vicinity) "r4rsyn"))
  92. (slib:load (in-vicinity (library-vicinity) "synrul"))
  93.  
  94. ;;; OK, time to build the databases.
  95. (initialize-scheme-syntactic-environment!)
  96.  
  97. ;;; MACRO:EXPAND is for you to use.  It takes an R4RS expression, macro-expands
  98. ;;; it, and returns the result of the macro expansion.
  99. (define (synclo:expand expression)
  100.   (set! *counter* 0)
  101.   (compile/top-level (list expression) scheme-syntactic-environment))
  102. (define macro:expand synclo:expand)
  103.  
  104. ;;; Here are EVAL, EVAL! and LOAD which expand macros.  You can replace the
  105. ;;; implementation's eval and load with them if you like.
  106. (define base:eval slib:eval)
  107. (define base:load load)
  108.  
  109. (define (synclo:eval x) (base:eval (macro:expand x)))
  110. (define macro:eval synclo:eval)
  111.  
  112. (define (synclo:load <pathname>)
  113.   (slib:eval-load <pathname> synclo:eval))
  114.  
  115. (define macro:load synclo:load)
  116.  
  117. (provide 'syntactic-closures)
  118. (provide 'macro)            ;Here because we may have
  119.                     ;(require 'sc-macro)
  120.